package team.seekers.hula.course.resource.problemSubmit.service;

import com.google.common.collect.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import team.seekers.hula.course.resource.problemSubmit.dao.SubmissionDao;
import team.seekers.hula.course.resource.problemSubmit.entity.dao.SubmissionDO;
import team.seekers.hula.course.resource.problemSubmit.entity.dto.SubmissionDTO;
import team.seekers.hula.course.resource.problemSubmit.entity.dto.SubmissionDetailDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class SubmissionService {

    @Autowired
    private SubmissionDao submissionDao;


    public void saveOrUpdateBatch(List<SubmissionDTO> submissions) {
        ArrayList<SubmissionDO> submissionDOS = new ArrayList<>();
        for (SubmissionDTO submission : submissions) {
            SubmissionDO submissionDO = new SubmissionDO();
            BeanUtils.copyProperties(submission,submissionDO);
            submissionDOS.add(submissionDO);
        }
        submissionDao.saveBatch(submissionDOS);
    }

    /**
     * 提交详情
     */
    public List<SubmissionDetailDTO> queryExamSubmissionDetail(Long userId, Long examId, Integer count) {
        List<SubmissionDO> submissionDOList = submissionDao.lambdaQuery()
                .eq(SubmissionDO::getUId, userId)
                .eq(SubmissionDO::getExId, examId)
                .eq(SubmissionDO::getCount, count).list();
        return Optional.of(SubmissionDO.toDTOList(submissionDOList)).orElse(Lists.newArrayList());
    }
}
